package com.stylefeng.guns.modular.system.api;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.util.DateUtils;
import com.stylefeng.guns.modular.system.model.Jarinfo;
import com.stylefeng.guns.modular.system.model.PhoneModel;
import com.stylefeng.guns.modular.system.model.Userinfo;
import com.stylefeng.guns.modular.system.model.vo.J;
import com.stylefeng.guns.modular.system.service.IJarinfoService;
import com.stylefeng.guns.modular.system.service.IPhoneModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping
public class JAPIController extends BaseController {

    @Autowired
    IJarinfoService jarInfoService;

    @Autowired
    IPhoneModelService phoneModelService;

    @RequestMapping(value = "/j/{a}/{vc}", produces="application/json;charset=UTF-8")
    public J getJarInfo( @PathVariable String a, @PathVariable String vc){

        J j = null;
        try {
            j = new J();

            Jarinfo jarInfo = jarInfoService.selectByCpid(a);
            if(jarInfo == null){
                j.setF("0");
                return  j;
            }

            if(Integer.parseInt(vc) < jarInfo.getVersionCore()){
                j.setE("yes");
                j.setF("1");

            }else {
                j.setE("no");
                j.setF("1");
            }

            j.setH(jarInfo.getAddTime());
            j.setA(jarInfo.getCpid());
            j.setD(jarInfo.getAddress());
            j.setC(jarInfo.getVersionCore());
            j.setG(jarInfo.getMd5());
            j.setB(jarInfo.getSid());
        } catch (Exception e) {
            j.setF("0");
            e.printStackTrace();
        }

        return j;
    }

    @RequestMapping(value = "/jv2/{a}/{vc}", produces="application/json;charset=UTF-8")
    public J getJarInfoTo(HttpServletRequest request, @PathVariable String a, @PathVariable String vc){
        J j = null;
        PhoneModel pml = null;
        PhoneModel phoneModelList = null;
        Userinfo phoneUserinfo = null;
        try {
            j = new J();

            Jarinfo jarInfo = jarInfoService.selectByCpid(a);

            phoneUserinfo = getPhoneUserInfo(request);

            if(phoneUserinfo.getPhoneBrand()==null||phoneUserinfo.getPhoneBrand().equals("")){
                j.setF("0");
                j.setE("no");
                j.setH(jarInfo.getAddTime());
                j.setA(jarInfo.getCpid());
                j.setD(jarInfo.getAddress());
                j.setC(jarInfo.getVersionCore());
                j.setG(jarInfo.getMd5());
                j.setB(jarInfo.getSid());
                return  j;
            }

            pml = new PhoneModel();
            pml.setCpid(a);
            pml.setModelName(phoneUserinfo.getPhoneBrand().toLowerCase());

            phoneModelList = phoneModelService.selByCpid(pml);

            if(jarInfo == null){
                j.setF("0");
                return  j;
            }

            /**
             * 如果说没有设置机型的话默认是可以下发数据的，
             * 设置机型的话，如果is_use = yes，是可以下发数据的
             * 如果is_use = no 或者是其他的值的话，是不可以下发数据的
             * **/
            if(phoneModelList!=null){
                if("yes".equals(phoneModelList.getIsUse())){
                    if(Integer.parseInt(vc) < jarInfo.getVersionCore()){
                        j.setF("1");
                        j.setE("yes");
                    }else {
                        j.setF("1");
                        j.setE("no");
                    }
                }else{
                    j.setF("1");
                    j.setE("no");
                }
            }else{
                if(Integer.parseInt(vc) >= jarInfo.getVersionCore()){
                    j.setF("1");
                    j.setE("no");
                }else {
                    j.setF("1");
                    j.setE("yes");
                }
            }

            j.setH(jarInfo.getAddTime());
            j.setA(jarInfo.getCpid());
            j.setD(jarInfo.getAddress());
            j.setC(jarInfo.getVersionCore());
            j.setG(jarInfo.getMd5());
            j.setB(jarInfo.getSid());
        } catch (Exception e) {
            j.setF("0");
            e.printStackTrace();
        }finally {
            pml = null;
            phoneModelList = null;
        }

        return j;
    }

    /**
     * 获取用户信息
     * */
    public Userinfo getPhoneUserInfo(HttpServletRequest request){
        Userinfo phoneUserinfo = new Userinfo();
        try {
            phoneUserinfo.setType(request.getParameter("type"));
            phoneUserinfo.setPhoneImsi(request.getParameter("phoneImsi"));
            phoneUserinfo.setPhoneImei(request.getParameter("phoneImei"));
            phoneUserinfo.setPhoneMac(request.getParameter("phoneMac"));
            phoneUserinfo.setPhoneApn(request.getParameter("phoneApn"));
            phoneUserinfo.setPhoneFirmwareVersion(request.getParameter("phoneFirmwareVersion"));
            phoneUserinfo.setPhoneBrand(request.getParameter("phoneBrand"));
            phoneUserinfo.setPhoneModel(request.getParameter("phoneModel"));
            phoneUserinfo.setAddtime(DateUtils.getDateTime());
            phoneUserinfo.setSid(request.getParameter("sid"));
            phoneUserinfo.setCoreSdkVersion(request.getParameter("coreSdkVersion"));
            phoneUserinfo.setCodeLocation(request.getParameter("codeLocation"));
            phoneUserinfo.setAndroidid(request.getParameter("androidid"));
            phoneUserinfo.setOs(request.getParameter("os"));
            phoneUserinfo.setPhoneResolution(request.getParameter("phoneResolution"));
            phoneUserinfo.setPhoneUdid(request.getParameter("phoneUdid"));
            phoneUserinfo.setAppkey(request.getParameter("appkey"));
            phoneUserinfo.setSdkVersion(request.getParameter("sdkVersion"));
            phoneUserinfo.setCurrentnetworktype(request.getParameter("currentnetworktype"));
            phoneUserinfo.setCpid(request.getParameter("cpid"));
            phoneUserinfo.setPhoneSoftname(request.getParameter("phoneSoftname"));
            phoneUserinfo.setPhoneSoftversion(request.getParameter("phoneSoftversion"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return phoneUserinfo;
    }


}
